package com.itheima.info.manage.system.service;

import com.itheima.info.manage.system.dao.BaseStudentDao;
import com.itheima.info.manage.system.dao.OtherStudentDao;
import com.itheima.info.manage.system.dao.StudentDao;
import com.itheima.info.manage.system.domain.Student;
import com.itheima.info.manage.system.factory.Factory;

public class StudentService {
  // private OtherStudentDao studentDao = new OtherStudentDao();
  private BaseStudentDao studentDao= Factory.getStudentDao();
    public boolean addStudent(Student student) {
        //调用dao

        return studentDao.addStudent(student);
    }

    public boolean isExist(String sid) {

        Student[] stus=studentDao.findAllStudents();//964   981
        // 遍历学生  判断是否相同
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            //对象调用方法前 进行 非空判断
            if(stu!=null&&stu.getSid().equals(sid)){
                return true;
            }

        }
        return false;
    }

    public Student[] findAllStudents() {
        Student[] stus = studentDao.findAllStudents();
        //判断数组是否有元素
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            //只要有一个人  那就认为数组 有数据
            if(stu!=null){
                return stus;
            }
        }
        //只有当数组中一个人都没有才能认为数组为空
        return null;


    }

    public void deleteStudentBySid(String sid) {
        studentDao.deleteStudentBySid(sid);
    }

    public void updateStudent(String sid, Student newStu) {
        studentDao.updateStudent(sid,newStu);
    }
}
